package org.csu.mypetstorebackstage.controller;

import org.csu.mypetstorebackstage.domain.LineItem;
import org.csu.mypetstorebackstage.domain.Order;
import org.csu.mypetstorebackstage.domain.OrderSales;
import org.csu.mypetstorebackstage.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    final String current = "order";
    @GetMapping("/allOrders")
    public String AllOrdersView(Model model,HttpSession session){

        List<Order> orderList=orderService.getAllOrders();
        model.addAttribute("orderList",orderList);
        model.addAttribute("current", current);
        for(int i=0;i<orderList.size();i++){
            System.out.println(orderList.get(i).getUsername());
        }
        session.setAttribute("orderList",orderList);
        return "order/AllOrders";
    }
    @GetMapping("/customerOrder")
    public String CustomerOrder( String userId, Model model){
        List<Order> orderList=orderService.getOrdersByUserId(userId);

        model.addAttribute("orderList",orderList);
        model.addAttribute("userId",userId);
        model.addAttribute("current", current);
        return "order/CustomerOrder";
    }
    @GetMapping("/Visualization")
    public String VisualiZation(){
        return  "order/VisualiZation";
    }
    @GetMapping("/deleteOrder")
    public String DeleteOrder(Model model,int orderId){
        orderService.deleteOrderByOrderId(orderId);
        List<Order> orderList=orderService.getAllOrders();
        model.addAttribute("orderList",orderList);
        model.addAttribute("current", current);

        return "order/AllOrders";
    }
    @GetMapping("/sendOut")
    public String SendOut(Model model,int orderId, HttpSession session){

       orderService.SendOrder(orderId);
        List<Order> orderList=orderService.getAllOrders();
        model.addAttribute("orderList",orderList);
        model.addAttribute("current", current);
        return "order/AllOrders";
    }
    @GetMapping("/toEditOrder")
    public String toEditOrder(int orderId,Model model,HttpSession session){
        Order order=orderService.getOrderByOrderId(orderId);
        System.out.println(order.getBillAddress1());
        model.addAttribute("order",order);
        model.addAttribute("current", current);
        session.setAttribute("order",order);

        return "order/EditOrder";
    }

    @PostMapping("/EditOrder")
    public String EditOrder(String cardType,String creditCard,String expiryDate,HttpSession session, Model model){
        Order order=(Order)session.getAttribute("order");
        System.out.println(order.getCardType());
        order.setCardType(cardType);
        order.setCreditCard(creditCard);
        order.setExpiryDate(expiryDate);
       if(orderService.modifyOrder(order)){
           model.addAttribute("message","修改成功!!!");
       }

       model.addAttribute("order",order);
       model.addAttribute("current", current);
       session.setAttribute("order",order);

        return "order/EditOrder";
    }
    @GetMapping("/returntoMain")
    public String ReturntoMain(){

        return "common/main";
    }
    @GetMapping("/lineItemShow")
    public String LineItemShow(int orderId,Model model,HttpSession session){
        /*Order order=(Order)session.getAttribute("order");
        System.out.println(order.getCardType());*/
        Order order=new Order();

        List<LineItem> lineItemList=orderService.getLineItemByOrderId(orderId);
        order.setLineItems(lineItemList);

        model.addAttribute("order",order);
        return "order/LineItem";
    }
    /*@RequestMapping( "/show")
    @ResponseBody
    public List<OrderSales> findById(Model model) {
        List<OrderSales> orders = orderService.getPriceList();
        System.err.println(orders.toString());
        return orders;
    }

    //展示柱状图
    @RequestMapping( "/showbar")
    public String show2() {

        return "Visual/bar";
    }*/

    //营业额
    @RequestMapping( "/showLine")
    @ResponseBody
    public  List<OrderSales>  findById(Model model){
        List<OrderSales> orders = orderService.getPriceList();
        System.err.println(orders.toString());
        return orders;

    }
    //订单数量
    @RequestMapping( "/showOrderAmount")
    @ResponseBody
    public  List<OrderSales>  getOrdersAmount(Model model){
        List<OrderSales> orders = orderService.getOrdersAmount();
        System.err.println(orders.toString());
        return orders;

    }
    //包裹数量
    @RequestMapping( "/showTotalAmount")
    @ResponseBody
    public  int   getAmount(Model model){
        List<Order> orders = orderService.getAllOrders();
        int amount=orders.size();
        System.err.println(amount);
        return amount;

    }

    @RequestMapping( "/show")
    public String show2() {

        return "Visual/VisualMain";
    }
}
